Imports System
Imports System.IO
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DotNetNuke

Namespace DNNQuickForm
    Partial Public Class FormBuilderEdit
        Inherits DotNetNuke.Entities.Modules.PortalModuleBase
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                If Not Page.IsPostBack Then
                    BindForms()
                    cmdDelete.Attributes.Add("onClick", "javascript:return confirm('Are You Sure You Wish To Delete This Item ?');")
                End If
                'Page.ClientScript.RegisterClientScriptInclude("FormDesigner", Page.ResolveUrl("~/DesktopModules/ActiveFormBuilder/scripts/FormDesigner.js"))
            Catch exc As Exception
                DotNetNuke.Services.Exceptions.ProcessModuleLoadException(Me, exc)
            End Try
        End Sub


        Private Sub CreateFile(ByVal Text As String, ByVal Name As String)
            If Trim(Text).Length > 0 Then
                Dim myFileStream As StreamWriter = Nothing
                Try
                    Dim strPath As String
                    strPath = Request.MapPath(PortalSettings.HomeDirectory) & "DNNQuickForms"
                    If Not (Directory.Exists(strPath)) Then
                        Directory.CreateDirectory(strPath)
                    End If
                    myFileStream = File.CreateText(strPath & "\" & Name.Replace(" ", "_") & ".ascx")
                    myFileStream.WriteLine(Text)
                    myFileStream.Close()
                Catch ex As Exception
                    'txtForm.Text = ex.ToString
                Finally
                    If Not myFileStream Is Nothing Then
                        myFileStream.Close()
                    End If
                End Try
            End If
        End Sub
        Private Sub cmdCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdCancel.Click
            Try
                Response.Redirect(Request.RawUrl, True)
            Catch exc As Exception
                DotNetNuke.Services.Exceptions.ProcessModuleLoadException(Me, exc)
            End Try
        End Sub


        Private Sub BindForms()
            Dim strPath As String
            strPath = Request.MapPath(PortalSettings.HomeDirectory) & "DNNQuickForms"
            If Not (Directory.Exists(strPath)) Then
                Directory.CreateDirectory(strPath)
                File.Copy(Request.MapPath("~/DesktopModules/DNNQuickForm/ContactForm.ascx"), strPath & "\ContactForm.ascx")
            End If
            Dim dirInfo As New DirectoryInfo(strPath)
            dgrdForms.DataSource = dirInfo.GetFiles("*.ascx")
            dgrdForms.DataBind()
        End Sub


        Private Sub dgrdForms_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrdForms.ItemCommand
            Dim fileName As String = dgrdForms.DataKeys(e.Item.ItemIndex)
            Dim myFSR As StreamReader
            Dim myFile As System.IO.FileInfo
            myFile = New System.IO.FileInfo(fileName)
            Dim strFileName As String
            strFileName = myFile.Name
            strFileName = strFileName.Substring(0, strFileName.Length - 5)
            strFileName = strFileName.Replace("_", " ")
            Try
                txtFormName.Text = strFileName
                myFSR = File.OpenText(fileName)
                txtForm.Text = myFSR.ReadToEnd()
                myFSR.Close()
            Catch ex As Exception
                'txtForm.Text = ex.ToString
            Finally
                If Not myFSR Is Nothing Then
                    myFSR.Close()
                End If
            End Try
            BindForms()
        End Sub

        Private Sub dgrdForms_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrdForms.DeleteCommand
            Dim fileName As String = dgrdForms.DataKeys(e.Item.ItemIndex)
            Try
                File.Delete(fileName)
                txtForm.Text = String.Empty
                txtFormName.Text = String.Empty
            Catch ex As Exception
                'txtForm.Text = ex.ToString
            End Try
            BindForms()
        End Sub



        Private Sub cmdSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSave.Click
            If Not txtForm.Text = String.Empty And Not txtFormName.Text = String.Empty Then
                CreateFile(txtForm.Text, txtFormName.Text)
                Response.Redirect(Request.RawUrl, True)
            End If
        End Sub
    End Class
End Namespace